package com.itmuch.cloud;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * 可以参考spring-cloud-netflix-core-1.2.3.RELEASE.jar包
 * org.springframework.cloud.netflix.zuul.filter的类
 */
public class PreZuulFilter extends ZuulFilter {
  private static final Logger LOGGER = LoggerFactory.getLogger(PreZuulFilter.class);

  /**
   * 是否启用这个filter
   * @return
   */
  @Override
  public boolean shouldFilter() {
    return true;
  }

  /**
   * 主要的执行逻辑
   * @return
   */
  @Override
  public Object run() {
    HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
    String host = request.getRemoteHost();
    LOGGER.info("请求的host:{}", host);
    return null;
  }

  /**
   * 过滤器的类型
   * @return
   */
  @Override
  public String filterType() {
    return "pre";
  }

  /**
   * 过滤器的顺序
   * @return
   */
  @Override
  public int filterOrder() {
    return 1;
  }

}
